package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;
import dto.DataManager;

/**
 * Servlet implementation class ChangePass
 */
@WebServlet("/ChangePass")
public class ChangePass extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
        String username =(String) session.getAttribute("username");
        DataManager dataManager = new DataManager();
        PrintWriter out = response.getWriter();
        User user = username ==null ? null : dataManager.getUserWithUserName(username);
        String oldPass = request.getParameter("oldPass");
        String newPass = request.getParameter("newPass");
        String newPassAgain = request.getParameter("newPassAgain");
        String status = "false";
        if(user.getPassword().equals(oldPass))
        {
        	if(newPass.equals(newPassAgain)&& !newPass.equals(oldPass))
        	{
        		if(dataManager.changePassword(user.getUserId(), newPass))
        			status = "true";
        	}
        }
        out.print(status);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
